/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Classes;
import vn.webapp.model.Classing;
import vn.webapp.model.CourseTeacherClassRaw;
import vn.webapp.model.Coursing;
import vn.webapp.model.CoursingTimeTabling;
import vn.webapp.model.CoursingsSave;

@Repository("coursingDAO")
//@SuppressWarnings({"unchecked", "rawtypes"})
public class CoursingDAOImpl extends BaseDao implements CoursingDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public List<Coursing> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Coursing.class, "coursing");
            criteria.createAlias("coursing.courses", "courses");
            criteria.createAlias("coursing.teachers", "teachers");
            criteria.createAlias("coursing.sessions", "sessions");
            
            List<Coursing> coursing = criteria.list();
            commit();
            return coursing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<CoursingsSave> getOriginSource()
    {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(CoursingsSave.class, "coursing");
            
            List<CoursingsSave> coursing = criteria.list();
            commit();
            return coursing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Coursing checkExists(int id, int teacherId, int courseId, int sessionId){
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Coursing.class, "coursing");
            criteria.createAlias("coursing.courses", "courses");
            criteria.add(Restrictions.eq("courses.id",courseId));
            criteria.createAlias("coursing.teachers", "teachers");
            criteria.add(Restrictions.eq("teachers.id",teacherId));
            criteria.createAlias("coursing.sessions", "sessions");
            criteria.add(Restrictions.eq("sessions.id",sessionId));
            criteria.add(Restrictions.ne("coursing.id",id));
            Coursing coursing = (Coursing)criteria.uniqueResult();
            commit();
            return coursing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
            
    @Override
    public int removeACoursing(int id){
        Coursing c = new Coursing();
        c.setCourseteacher_id(id);        
        try {
            begin();
            getSession().delete(c);
            commit();
            return 1;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public int saveACoursing(CoursingsSave coursing)
    {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(coursing);
           commit();
           return id;           
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public void editACoursing(CoursingsSave coursing){
        try {
           begin();
           getSession().update(coursing);
           commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
        } finally {
            flush();
            close();
        }
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Coursing> filter(int page, int pageSize) {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Coursing.class, "coursing");
            criteria.createAlias("coursing.courses", "courses");
            criteria.createAlias("coursing.teachers", "teachers");
            criteria.createAlias("coursing.sessions", "sessions");
            
            //Criteria criteria = getSession().createCriteria(Coursing.class);
            criteria.setMaxResults(pageSize);
            if (page > 0) {
                criteria.setFirstResult(page * pageSize);
            } else {
                criteria.setFirstResult(page);
            }
            List<Coursing> coursing = criteria.list();
            commit();
            return coursing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<Coursing> getById(int coursingId){
        try {
        begin();
        Criteria criteria = getSession().createCriteria(Coursing.class, "coursing");
        List<Coursing> coursing = new ArrayList<>();
        
        criteria.add(Restrictions.eq("coursing.courseteacher_id", coursingId));
        coursing = (List<Coursing>) criteria.list();
        commit();
        return coursing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<CourseTeacherClassRaw> listCourseTeacherClassById(int iCourseTeacherClassId)
    {
        try {
        begin();
        Criteria criteria = getSession().createCriteria(CourseTeacherClassRaw.class, "courseteacherclass");
        List<CourseTeacherClassRaw> courseteacherclass = new ArrayList<>();
        
        criteria.add(Restrictions.eq("courseteacherclass.courseteacher_id", iCourseTeacherClassId));
        courseteacherclass = (List<CourseTeacherClassRaw>) criteria.list();
        commit();
        return courseteacherclass;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public CoursingTimeTabling getByIdTime(int coursingTimeId){
        try {
        begin();
        Criteria criteria = getSession().createCriteria(CoursingTimeTabling.class, "coursing");
        
        criteria.add(Restrictions.eq("coursing.courseteacher_id", coursingTimeId));
        CoursingTimeTabling coursing = (CoursingTimeTabling) criteria.uniqueResult();
        commit();
        return coursing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public List<CoursingTimeTabling> getByIdTimetabling(int coursingId){
        try {
        begin();
        Criteria criteria = getSession().createCriteria(CoursingTimeTabling.class, "coursingt");
        List<CoursingTimeTabling> coursingt = new ArrayList<>();
        
        criteria.add(Restrictions.eq("coursingt.courseteacher_id", coursingId));
        coursingt = (List<CoursingTimeTabling>) criteria.list();
        commit();
        return coursingt;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public int count(){
        //int size = 0;
         try {
            begin();
            Criteria criteria = getSession().createCriteria(Coursing.class);            
            List<Coursing> coursing = criteria.list();
            commit();
            return coursing.size();
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Coursing getByTeacherCourseSession(int teacherId, int courseId, int sessionId)
    {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Coursing.class, "coursing");
            criteria.createAlias("coursing.courses", "courses");
            criteria.add(Restrictions.eq("courses.id",courseId));
            criteria.createAlias("coursing.teachers", "teachers");
            criteria.add(Restrictions.eq("teachers.id",teacherId));
            criteria.createAlias("coursing.sessions", "sessions");
            criteria.add(Restrictions.eq("sessions.id",sessionId));
            Coursing coursing = (Coursing)criteria.uniqueResult();
            commit();
            return coursing;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
}
